package com.lanchetech.admin.controller;

import com.lanchetech.admin.aop.OperatorLog;
import com.lanchetech.admin.service.UserService;
import com.lanchetech.bean.request.*;
import com.lanchetech.bean.response.*;
import com.lanchetech.bean.vo.*;
import com.lanchetech.common.exception.ResultCodeException;
import com.lanchetech.entity.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

@Api(value = "用户管理接口", tags = "用户管理接口")
@RequestMapping("/api/user")
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @ApiOperation("获取用户详细信息")
    @PreAuthorize("hasAuthority('user.detail')")
    @GetMapping("/getUserDetail")
    public ResultData<UserDetailResp> getUserDetail(@RequestParam("id") Long id) {
        return userService.getUserDetail(id);
    }

    @OperatorLog("修改用户身份信息")
    @ApiOperation(value = "修改用户身份信息")
    @PreAuthorize("hasAuthority('user.detail.modify.auth')")
    @PostMapping("/modifyUserInfo")
    public BaseResp modifyUserInfo(@RequestBody UserInfo userInfo) {
        return userService.modifyUserInfo(userInfo);
    }


    @OperatorLog("拉黑用户或取消拉黑用户")
    @ApiOperation("拉黑用户或取消拉黑用户")
    @PreAuthorize("hasAuthority('user.detail.modify.auth')")
    @GetMapping("/blockUser")
    public BaseResp blockUser(@RequestParam("id") Long id, @RequestParam("status") Integer status) {
        return userService.blockUser(id, status);
    }

    @ApiOperation("获取所有用户分页")
    @PreAuthorize("hasAuthority('user.index')")
    @PostMapping("/getUserPage")
    public ResultData<BasePageResp<User>> getUserPage(@RequestBody PageReq req) {
        return userService.getUserPage(req);
    }

    @OperatorLog(value = "添加用户特权")
    @ApiOperation(value = "添加用户特权")
    @PreAuthorize("hasAuthority('user.detail.privilege.auth')")
    @PostMapping("/createUserPrivilege")
    public BaseResp createUserPrivilege(@RequestBody Privilege privilege) {
        return userService.createUserPrivilege(privilege);
    }

    @OperatorLog(value = "删除用户特权")
    @ApiOperation(value = "删除用户特权")
    @PreAuthorize("hasAuthority('user.detail.privilege.auth')")
    @GetMapping("/deleteUserPrivilege")
    public BaseResp deleteUserPrivilege(@RequestParam("id") Long id) {
        return userService.deleteUserPrivilege(id);
    }

    @ApiOperation("获取提现记录分页")
    @PreAuthorize("hasAuthority('user.withdraw')")
    @PostMapping("/getWithdrawPage")
    public ResultData<BasePageResp<WithdrawResp>> getWithdrawPage(@RequestBody WithdrawReq req) {
        return userService.getWithdrawPage(req);
    }

    @ApiOperation(value = "导出提现记录分页Excel")
    @PreAuthorize("hasAuthority('user.withdraw.export.auth')")
    @PostMapping("/exportWithdrawExcel")
    public void exportWithdrawExcel(@RequestBody WithdrawReq req, HttpServletResponse response) throws IOException {
        userService.exportWithdrawExcel(req, response);
    }

    @OperatorLog("用户提现审核")
    @ApiOperation("用户提现审核")
    @PreAuthorize("hasAuthority('user.withdraw.check.auth')")
    @PostMapping("/checkWithdraw")
    public BaseResp checkWithdraw(@RequestBody Withdraw withdraw) {
        return userService.checkWithdraw(withdraw);
    }

    @ApiOperation("获取用户上下级关系分页")
    @PreAuthorize("hasAuthority('user.relationship')")
    @PostMapping("/getUserRelationshipPage")
    public ResultData<BasePageResp<RelationshipResp>> getUserRelationshipPage(@RequestBody RelationshipReq req) {
        return userService.getUserRelationshipPage(req);
    }

    @ApiOperation("导出用户列表Excel")
    @PreAuthorize("hasAuthority('user.index.export.auth')")
    @PostMapping("/exportUserExcel")
    public void exportUserExcel(@RequestBody PageReq req, HttpServletResponse response) throws IOException {
        userService.exportUserExcel(req, response);
    }

    @ApiOperation("获取佣金发放记录分页")
    @PreAuthorize("hasAuthority('user.commission')")
    @PostMapping("/getUserCommissionPage")
    public ResultData<BasePageResp<UserCommissionResp>> getUserCommissionPage(@RequestBody UserCommissionReq req) {
        return userService.getUserCommissionPage(req);
    }

    @OperatorLog("给经销商发放消费佣金")
    @ApiOperation("给经销商发放消费佣金")
    @PreAuthorize("hasAuthority('user.relationship.commission.auth')")
    @PostMapping("/issueConsumeCommission")
    public BaseResp issueConsumeCommission(@RequestBody UserCommission userCommission) {
        return userService.issueConsumeCommission(userCommission);
    }

    @OperatorLog("扣除用户佣金")
    @ApiOperation("扣除用户佣金")
    @PreAuthorize("hasAuthority('user.commission.edit.auth')")
    @GetMapping("/cancelCommission")
    public BaseResp cancelCommission(@RequestParam("id") Long id) {
        return userService.cancelCommission(id);
    }

    @ApiOperation("获取用户余额流水")
    @PostMapping("/getBalanceFlowPage")
    public ResultData<BasePageResp<BalanceFlowVO>> getBalanceFlowPage(@RequestBody PageReq req) {
        return userService.getBalanceFlowPage(req);
    }

    @OperatorLog("为用户添加下级")
    @ApiOperation("为用户添加下级")
    @PreAuthorize("hasAuthority('user.detail.relationship.auth')")
    @GetMapping("/createUserRelationship")
    public BaseResp createUserRelationship(@RequestParam("userId") Long userId, @RequestParam("phone") String phone) {
        return userService.createUserRelationship(userId, phone);
    }

    @OperatorLog("锁定用户上下级关系")
    @ApiOperation("锁定用户上下级关系")
    @PreAuthorize("hasAuthority('user.detail.lock.auth')")
    @GetMapping("/lockUserRelationship")
    public BaseResp lockUserRelationship(@RequestParam("userId") Long userId) {
        return userService.lockUserRelationship(userId);
    }

    @ApiOperation("获取我的团队分页")
    @PreAuthorize("hasAuthority('user.user-group')")
    @PostMapping("/getUserGroupPage")
    public ResultData<BasePageResp<UserGroupVO>> getUserGroupPage(@RequestBody UserGroupReq req) {
        return userService.getUserGroupPage(req);
    }

    @OperatorLog("删除我的团队一员")
    @ApiOperation("删除我的团队一员")
    @PreAuthorize("hasAuthority('user.user-group.edit.auth')")
    @GetMapping("/deleteUserGroup")
    public BaseResp deleteUserGroup(@RequestParam("id") Long id) {
        return userService.deleteUserGroup(id);
    }

    @OperatorLog("更换团长")
    @ApiOperation("更换团长")
    @PreAuthorize("hasAuthority('user.user-group.edit.auth')")
    @GetMapping("/changeCaptain")
    public BaseResp changeCaptain(@RequestParam("id") Long id, @RequestParam("captainId") Long captainId) {
        return userService.changeCaptain(id, captainId);
    }

    @OperatorLog("团长转让")
    @ApiOperation("团长转让")
    @PreAuthorize("hasAuthority('user.user-group.edit.auth')")
    @GetMapping("/transferCaptain")
    public ResultData<TransferUserGroupVO> transferCaptain(@RequestParam("oldCaptainId") Long oldCaptainId, @RequestParam("newCaptainId") Long newCaptainId) {
        return userService.transferCaptain(oldCaptainId, newCaptainId);
    }

//    @OperatorLog("编辑嗨币")
//    @ApiOperation("编辑嗨币")
//    @PreAuthorize("hasAuthority('user.detail.edit-card-point.auth')")
//    @GetMapping("/updateCardPoint")
//    public BaseResp updateCardPoint(@RequestParam("userId") Long userId, @RequestParam("amount") BigDecimal amount) {
//        return userService.updateCardPoint(userId, amount);
//    }

    @OperatorLog("添加或修改商圈合作")
    @ApiOperation("添加或修改商圈合作")
    @PreAuthorize("hasAuthority('shop.business-review.audit.auth')")
    @PostMapping("/editShopApply")
    public BaseResp createInformation(@RequestBody ShopApply shopApply) {
        return userService.editShopApply(shopApply);
    }

    @ApiOperation("获取商圈合作分页")
    @PreAuthorize("hasAuthority('shop.business-review')")
    @PostMapping("/getShopApplyPage")
    public ResultData<BasePageResp<ShopApplyVO>> getShopApplyPage(@RequestBody PageReq req) {
        return userService.getShopApplyPage(req);
    }

    @ApiOperation("审核商圈合作")
    @PreAuthorize("hasAuthority('shop.business-review.audit.auth')")
    @PostMapping("/auditShopApply")
    public BaseResp auditShopApply(@RequestBody ShopApply req) {
        return userService.auditShopApply(req);
    }

    @OperatorLog("删除商圈合作")
    @PreAuthorize("hasAuthority('shop.business-review.audit.auth')")
    @ApiOperation("删除商圈合作")
    @GetMapping("/deleteShopApply")
    public BaseResp deleteShopApply(@RequestParam("id") Long id) {
        return userService.deleteShopApply(id);
    }

    @ApiOperation("获取联系我们的用户信息")
    @PostMapping("/getInformationPage")
    public BaseResp getInformationPage(@RequestBody PageReq req) {
        return userService.getInformationPage(req);
    }

    @OperatorLog("编辑广告余额")
    @ApiOperation("编辑广告余额")
    @PreAuthorize("hasAuthority('user.detail.edit-ad-point.auth')")
    @GetMapping("/updateAdPoint")
    public BaseResp updateAdPoint(@RequestParam("userId") Long userId, @RequestParam("amount") BigDecimal amount, @RequestParam("tradeType") Byte tradeType) {
        return userService.updateAdPoint(userId, amount, tradeType);
    }

    @OperatorLog("设置用户为省市区代理")
    @ApiOperation("设置用户为省市区代理")
    @PreAuthorize("hasAuthority('user.agent.edit.auth')")
    @PostMapping("/editUserAgentLevel")
    public BaseResp editUserAgentLevel(@RequestBody UserAgentLevel req) {
        return userService.editUserAgentLevel(req);
    }

    @OperatorLog("取消用户的省市区代理")
    @ApiOperation("取消用户的省市区代理")
    @PreAuthorize("hasAuthority('user.agent.edit.auth')")
    @GetMapping("/cancelUserAgentLevel")
    public BaseResp cancelUserAgentLevel(@RequestParam("id") Long id) {
        return userService.cancelUserAgentLevel(id);
    }

    @ApiOperation("获取用户代理分页")
    @PreAuthorize("hasAuthority('user.agent')")
    @PostMapping("/getUserAgentLevelPage")
    public ResultData<BasePageResp<UserAgentLevelVO>> getUserAgentLevelPage(@RequestBody PageReq req) {
        return userService.getUserAgentLevelPage(req);
    }

    @ApiOperation("获取获取通用审批分页")
    @PostMapping("/getTicketPage")
    public ResultData<BasePageResp<TicketVO>> getTicketPage(@RequestBody TicketPageReq req) {
        return userService.getTicketPage(req);
    }

    @ApiOperation("编辑通用审批")
    @PostMapping("/editTicket")
    public BaseResp editTicket(@RequestBody Ticket req) {
        return userService.editTicket(req);
    }

    @ApiOperation("创建审核身份")
    @PostMapping("/createUserLevelTicket")
    public BaseResp createUserLevelTicket(@RequestBody VipReq req) {
        return userService.createUserLevelTicket(req);
    }

    @ApiOperation("获取通用审批详情")
    @GetMapping("/getTicketDetail")
    public ResultData<TicketVO> getTicketDetail(@RequestParam("id") Long id) {
        return userService.getTicketDetail(id);
    }

    @ApiOperation("删除通用审批")
    @GetMapping("/deleteTicket")
    public BaseResp deleteTicket(@RequestParam("id") Long id) {
        return userService.deleteTicket(id);
    }

    @ApiOperation("初审通用审批")
    @PreAuthorize("hasAuthority('user.ticket.first-audit.auth')")
    @PostMapping("/firstAuditTicket")
    public BaseResp firstAuditTicket(@RequestBody Ticket req) {
        return userService.firstAuditTicket(req);
    }

    @ApiOperation("复审通用审批")
    @PreAuthorize("hasAuthority('user.ticket.second-audit.auth')")
    @PostMapping("/secondAuditTicket")
    public BaseResp secondAuditTicket(@RequestBody Ticket req) {
        return userService.secondAuditTicket(req);
    }

    @ApiOperation("获取区域代理申请分页")
    @PostMapping("/getAreaApplyPage")
    public ResultData<BasePageResp<AreaApplyVO>> getAreaApplyPage(@RequestBody AreaApplyPageReq req) {
        return userService.getAreaApplyPage(req);
    }

    @ApiOperation("审核区域代理申请")
    @PostMapping("/auditAreaApply")
    public BaseResp auditAreaApply(@RequestBody AreaApply req) {
        return userService.auditAreaApply(req);
    }

    @ApiOperation("获取区域审核详情")
    @GetMapping("/getAreaApplyDetail")
    public ResultData<AreaApplyVO> getAreaApplyDetail(@RequestParam("id") Long id) {
        return userService.getAreaApplyDetail(id);
    }

    @ApiOperation("获取意见反馈分页")
    @PostMapping("/getSuggestionPage")
    public ResultData<BasePageResp<SuggestionVO>> getSuggestionPage(@RequestBody PageReq req) {
        return userService.getSuggestionPage(req);
    }

    @ApiOperation("设置用户的员工属性")
    @GetMapping("/updateUserStaffType")
    public BaseResp updateUserStaffType(@RequestParam("id") Long id, @RequestParam("staffType") Byte staffType) {
        return userService.updateUserStaffType(id, staffType);
    }

    @ApiOperation("获取赠送身份记录")
    @PostMapping("/getGiveIdentityRecordPage")
    public BaseResp getGiveIdentityRecordPage(@RequestBody GiveIdentityRecordPageReq req) {
        return userService.getGiveIdentityRecordPage(req);
    }

    @ApiOperation("获取用户银行卡号列表")
    @GetMapping("/getUserBankAccount")
    public ResultData<List<BankAccount>> getUserBankAccount(@RequestParam("userId") Long userId) {
        return userService.getUserBankAccount(userId);
    }

    @ApiOperation("添加银行卡账号")
    @PostMapping("/editBankAccount")
    public BaseResp editBankAccount(@RequestBody BankAccount bankAccount) {
        return userService.editBankAccount(bankAccount);
    }

    @ApiOperation("获取聊天用户信息")
    @GetMapping("/getMsgUserDetail")
    public ResultData<MsgUser> getMsgUserDetail(@RequestParam("userId") Long userId) {
        return userService.getMsgUserDetail(userId);
    }

    @ApiOperation("获取主管团队业绩分页")
    @PostMapping("/getGroupsPerformancePage")
    public ResultData<BasePageResp<GroupsPerformanceVO>> getGroupsPerformancePage(@RequestBody GroupsPerformancePageReq req) {
        return userService.getGroupsPerformancePage(req);
    }

    @ApiOperation("获取合伙人和分公司团队业绩记录分页")
    @PostMapping("/getShareholderPerformancePage")
    public ResultData<BasePageResp<ShareholderPerformanceVO>> getShareholderPerformancePage(@RequestBody ShareholderPerformancePageReq req) {
        return userService.getShareholderPerformancePage(req);
    }

    @ApiOperation("获取福利补贴分页")
    @PostMapping("/getWelfareFlowPage")
    public ResultData<BasePageResp<WelfareFlowVO>> getWelfareFlowPage(@RequestBody WelfareFlowPageReq req) {
        return userService.getWelfareFlowPage(req);
    }

    @ApiOperation("达人申请列表")
//    @PreAuthorize("hasAuthority('celebrity.celebrity-list')")
    @PostMapping("/celebrityList")
    public ResultData<BasePageResp<UserCelebrityVo>> celebrityList(@RequestBody UserCelebrityReq req) {
        return userService.celebrityList(req);
    }

    @ApiOperation("达人审核")
//    @PreAuthorize("hasAuthority('celebrity.celebrity-audit.auth')")
    @PostMapping("/celebrityExamine")
    public BaseResp celebrityExamine(@RequestBody UserCelebrity userCelebrity) {
        BaseResp baseResp = new BaseResp();
        try {
            userService.celebrityExamine(userCelebrity);
        } catch (ResultCodeException e) {
            baseResp.setResultCode(e.getResultCode());
        }
        return baseResp;
    }
}
